/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.interceptor;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.model.po.Member;
import com.eyaomall.wxmall.service.MemberService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.common.helper.GlobEnv;
/**
 * 订单拦截器
 * @author pro
 *
 */
public class OrderInterceptor extends HandlerInterceptorAdapter {
	@Autowired
	private MemberService memberService;
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if(MemberHelper.getId() == null || MemberHelper.getId() <= 0){
			String shopId = request.getParameter("shopId");
			if(StringUtil.isNotEmpty(shopId)){
				response.sendRedirect(GlobEnv.getWebURL("verify") + "?shopId" + shopId);
			}else{
				response.sendRedirect(GlobEnv.getWebURL("error/error"));
			}
			return false;
		}
		
		Member member = new Member();
		member.setId(MemberHelper.getId());
		member = memberService.query(member);
		if(member != null && StringUtil.isEmpty(member.getPhone())){
			
			String redirectUrl = request.getRequestURL().toString();
			if(StringUtil.isNotEmpty(request.getQueryString())){
				redirectUrl += "?" + request.getQueryString();
			}
			
			redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
			response.sendRedirect(GlobEnv.getWebURL("member/bind") + "?redirect=" + redirectUrl);
			return false;
		}
		
		return true;
	}
}
